package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Menus;
import com.itheima.pojo.Permission;
import com.itheima.service.MenuService;
import org.springframework.security.web.server.util.matcher.MediaTypeServerWebExchangeMatcher;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.plaf.PanelUI;
import java.util.List;
import java.util.Map;

/**
 * @Author zhushaoie
 * @Date 2021/6/28 22:54
 * @Version 1.0
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        try {
            menuService.add(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_fail);
        }
        return new Result(true, MessageConstant.ADD_succeed);
    }

    @RequestMapping("/findByName")
    public Result findByName() {
        try {
            List<Map<String, Integer>> all = menuService.findByName();
            return new Result(true, MessageConstant.SELECT_succeed, all);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SELECT_fail);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return menuService.pageQuery(queryPageBean);

    }

    @RequestMapping("/findChildrenMenu")
    public Result findChildrenMenu(Integer parentMenuId) {
        try {
            List<Menu> menu = menuService.findChildrenMenu(parentMenuId);
            return new Result(true, MessageConstant.SELECT_succeed, menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.SELECT_fail);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        try {
            menuService.edit(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDETE_fail);
        }
        return new Result(true, MessageConstant.UPDETE_succeed);
    }

    @RequestMapping("/findRoleBymid")
    public Result delete(Integer id) {
        try {
            menuService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_fail);
        }
        return new Result(true, MessageConstant.DELETE_succeed);
    }

    @RequestMapping("/findMenuByUsername")
    public Result findMenuByUsername(String username) {

        try {
            List<Menus> menus = menuService.findMenuByUsername(username);
            return new Result(true, MessageConstant.SELECT_succeed, menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SELECT_fail);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Menu> list = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


}
